﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wheel.Models;
using System.Text;

namespace wheel.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            GameInfo.Reset();
            return View();
        }


        public ActionResult Reset()
        {
            //GameInfo.Reset();
            return new EmptyResult();
        }

        public ActionResult SolvePuzzle(string guess, int potentialPoints)
        {
            bool guessedIt = guess.ToLower().Equals(GameInfo.Current.WordToFind.Definition.ToLower());
            if (guessedIt)
                wheel.Models.GameInfo.Current.UserChoices.Add(new UserChoice() { Letter = 'A', Score = potentialPoints * 10, Time = DateTime.Now });
            return new JsonResult() { Data =  guessedIt, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public ActionResult GuessLetter(char c, int potentialPoints, int playerIndex)
        {
            var choice = new UserChoice() { Letter = char.ToLower(c), Time = DateTime.Now };
            if (!GameInfo.Current.UserChoices.Any(ch => ch.Letter == char.ToLower(c)))
                GameInfo.Current.UserChoices.Add(choice);
            
            StringBuilder sb = new StringBuilder();
            bool found = false;
            int numFound = 0;
            foreach (char ch in GameInfo.Current.WordToFind.Definition.ToLower())
            {
                found = false;
                if (ch == ' ')
                {
                    sb.Append(' ');
                    found = true;
                }
                else
                {
                    for (int i = 0; i < GameInfo.Current.UserChoices.Count(); i++)
                    {
                        if (ch == GameInfo.Current.UserChoices[i].Letter)
                        {
                            sb.Append(ch);
                            found = true;
                            if (ch == char.ToLower(c))
                                numFound++;
                        }
                    }
                }
                if (!found)
                    sb.Append("#");

            }
            choice.Score += potentialPoints;
            
            return new JsonResult() { Data = new { lettersFound = numFound, word = sb.ToString(), letter = c }};
        }

        public ActionResult AIGuessLetter(int potentialPoints, int playerIndex)
        {
            var possibleletters = GetPossibleLetters();

            if(possibleletters.Count ==1)
                return GuessLetter(possibleletters[0], 0, playerIndex);
            else
                return GuessLetter(possibleletters[new Random().Next(possibleletters.Count)], 0, playerIndex);
        }

        private List<char> GetPossibleLetters()
        {
            var possibleletters = new List<char>();
            for (char leter = 'a'; leter <= 'z'; leter++)
            {
                bool used = false;
                foreach (var xx in wheel.Models.GameInfo.Current.UserChoices)
                    if (xx.Letter.ToString().ToLower() == leter.ToString())
                    {
                        used = true;
                        break;
                    }
                if (!used)
                    possibleletters.Add(leter);
            }
            return possibleletters;
        }

        public ActionResult GetNonSelectedLetters()
        {
            var possibleletters = GetPossibleLetters();

            return Json(string.Join("", possibleletters));
        }
    }
}
